package com.four.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.four.common.utils.PageUtils;
import com.four.common.utils.R;
import com.four.common.vo.CommodityTo;
import com.four.common.vo.ShoppingVo;
import com.four.order.dao.LogisticsDao;
import com.four.order.dao.ShopExpressDao;
import com.four.order.dao.ShoppingDao;
import com.four.order.entity.OrLogistics;
import com.four.order.entity.OrShopExpress;
import com.four.order.entity.OrShopping;
import com.four.order.feign.SeckillFeign;
import com.four.order.service.ShoppingService;
import com.four.order.vo.LogisticsVo;
import com.four.order.vo.ShoppingPage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:lky
 * @Date: 2022/01/05/18:39
 * @Description
 */
@Service
public class ShoppingServiceImpl implements ShoppingService {

    @Autowired
    private ShoppingDao shoppingDao;
    @Autowired
    private LogisticsDao logisticsDao;
    @Autowired
    private ShopExpressDao shopExpressDao;
    @Autowired
    private SeckillFeign seckillFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 添加订单
     * @param shoppingVo
     * @return
     */
    @Override
    public R addShopping(ShoppingVo shoppingVo) {

        shoppingDao.addShopping(shoppingVo);
        rabbitTemplate.convertAndSend("order-die", JSON.toJSONString(shoppingVo));
        return R.ok().put("orId",shoppingVo.getOrId());
    }

    /**
     * 列表
     * @param shoppingPage
     * @return
     */
    @Override
    public R findAllShopping(ShoppingPage shoppingPage) {
        List<OrShopping> allShopping = shoppingDao.findAllShopping(shoppingPage);
        PageUtils pageUtils = pageList(allShopping, shoppingPage.getPage(), shoppingPage.getSize());
        return R.ok().put("data",pageUtils);
    }

    /**
     * 进行分页
     * @param list
     * @param page
     * @param limit
     * @return
     */
    private PageUtils pageList(List<OrShopping> list, Integer page, Integer limit) {
        //找到并提取响应的页面的数据
        List<OrShopping> shoppings = new ArrayList<>();
        //总数
        Integer sum = 0;
        //计算上页的最后一条
        Integer preObj = page - 1 != 0 ? page * limit - 1: page - 1;
        //计算这页的结尾条
        Integer nextObj = page * limit;
        for (OrShopping shopping : list) {
            //循环一次数量加1
            sum++;
            if (sum>preObj && sum<=nextObj) {
                shoppings.add(shopping);
            }
        }
        return new PageUtils(shoppings,list.size(),page,limit);
    }

    /**
     * 查看订单
     * @param orId
     * @return
     */
    @Override
    public R getShowStatus(Integer orId) {
        //查询订单详情
        OrShopping shopping = shoppingDao.getById(orId);
        //查询快递
        OrShopExpress byOrId = shopExpressDao.getByOrId(shopping.getOrId());
        //查询物流
        List<OrLogistics> shopExpresses = logisticsDao.getByExpressId(byOrId.getExpressId());
        //查询商品信息
        CommodityTo byCommId = seckillFeign.getByCommId(shopping.getComId());

        //整合
        LogisticsVo vo = new LogisticsVo();
        vo.setShopping(shopping);
        vo.setShopExpress(byOrId);
        vo.setLogistics(shopExpresses);
        vo.setCommodityTo(byCommId);

        return R.ok().put("data",vo);
    }

    /**
     * 修改订单状态为已收货
     * @param orId
     * @return
     */
    @Override
    public R updateShowStatus(Integer orId) {
        OrShopping byId = shoppingDao.getById(orId);
        if (byId==null) {
            return R.error(500,"该订单已失效");
        }
        byId.setOrStatus(3);
        shoppingDao.updateByStatus(byId);
        return R.ok();
    }
}
